<?php
class SaveTheme extends Action {
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$theme = rqsts ( array ('theme_id' => 0, 'name', 'note' ), true );
		$themeModel = new WebThemeModel ();
		if (empty ( $theme ['theme_id'] )) { // 新增
			unset ( $theme ['theme_id'] );
		}
		//验证用户数据的合法性
		$valid = $this->validate ( 'theme', $theme, $themeModel );
		if ($valid === true) { //用户数据合法	
			$theme ['selected'] = rqst ( 'selected' ) == 'on' ? 1 : 0;
			$rst = $themeModel->save ( $theme );
			if (! empty ( $rst )) {
				if ($theme ['selected']) {
					$themeModel->setDefault ( $rst ['theme_id'] );
				}
				if (! rqset ( 'goon' )) {
					sess_add ( 'page_tip_info', '<strong>恭喜,</strong>主题保存成功.' );
					Response::redirect ( './?Ctlr=EditTheme&suc=1&tid=' . $rst ['theme_id'] );
				} else {
					Response::redirect ( './?Ctlr=AddTheme' );
				}
			} else {
				sess_add ( 'theme_info', $theme );
				sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $themeModel->last_error_msg () );
				Response::redirect ( $this->referer );
			}
		}
		sess_add ( 'theme_error', $valid );
		sess_add ( 'theme_info', $theme );
		Response::redirect ( $this->referer );
	}
}